import { BaseService } from './base.service';
import { Injectable } from '@angular/core';
import { CacheService } from './cache.service';
import { myValidNull } from '../util/string-utils';
import { HttpClient } from '@angular/common/http';

/**
 * 系统配置管理服务
 */
@Injectable()
export class SysParamService extends BaseService {

  constructor(http: HttpClient, cacheService: CacheService) {
    super(http, cacheService);
  }

  baseUrl: any = 'api/params';

  /**
   * 获取 系统配置列表
   *
   * @param page
   * @returns {Promise<any>}
   */
  pagination(page, keyword): Promise<any> {
    let url = this.baseUrl + '/pagination';
    page['_filter_eq_status'] = 1;
    if (keyword) {
      page['_filter_like_keyword'] = keyword;
    }
    return this.post(url, page);
  }

  saveOrUpdateEntity(entity): Promise<any> {
    if (myValidNull(entity.id)) {
      return this.post(this.baseUrl + '/add', entity);
    } else {
      return this.post(this.baseUrl + '/modify/' + entity.id, entity);
    }
  }

  load(id): Promise<any> {
    return this.get(this.baseUrl + '/' + id);
  }

  loadByCode(code): Promise<any> {
    return this.get(this.baseUrl + '/actions/' + code);
  }

  loadByCodes(codes): Promise<any> {
    return this.get(this.baseUrl + '/actions/params/' + codes);
  }

  deleteObj(ids): Promise<any> {
    return this.delete(this.baseUrl + '/', ids);
  }
}
